package com.atguigu.java;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 *      RandomAccessFile的使用
 *          1.RandomAccessFile直接继承于java.lang.Object类，实现了DataInput和DataOutput接口
 *          2.RandomAccessFile既可以作为一个输入流，又可以作为一个输出流
 *
 *          3.如果RandomAccessFile作为输出流时，
 *              写出到的文件如果不存在，则在执行过程中自动创建
 *              如果写出到的文件存在，则会对原有文件内容进行覆盖。(默认情况下，是从头覆盖)
 *
 *          4.可以通过相关的操作，实现RandomAccessFile"插入"数据的效果
 *
 *
 * @author : leimingchao
 * @date Date : 2021年10月10日 16:42
 */
public class RandomAccessFileTest {

    @Test
    public void test1() {
        RandomAccessFile randomAccessFile1 = null;
        RandomAccessFile randomAccessFile2 = null;
        try {
            randomAccessFile1 = new RandomAccessFile(new File("爱情与友情.jpg"),"r");
            randomAccessFile2 = new RandomAccessFile(new File("爱情与友情1.jpg"),"rw");

            byte[] arr = new byte[1024];
            int len;
            while ((len = randomAccessFile1.read(arr)) != -1) {
                randomAccessFile2.write(arr,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (randomAccessFile2 != null) {

                try {
                    randomAccessFile2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (randomAccessFile1 != null) {

                try {
                    randomAccessFile1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void test2() {
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile("hello.txt","rw");
            //将指针调到角标为3的位置
            randomAccessFile.seek(3);
            //
            randomAccessFile.write("xyz".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (randomAccessFile != null) {

                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 使用RandomAccessFile实现数据的插入效果
     */
    @Test
    public void test3() {
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile("hello.txt","rw");
            //将指针调到角标为3的位置
            randomAccessFile.seek(3);
            //保存指针3后面的所有数据到StringBuilder中
            StringBuilder sb = new StringBuilder((int) new File("hello.txt").length());
            byte[] arr = new byte[20];
            int len;
            while ((len = randomAccessFile.read(arr)) != -1) {
                sb.append(new String(arr,0,len));
            }
            //调回指针，写入"xyz"
            randomAccessFile.seek(3);
            randomAccessFile.write("xyz".getBytes());
            //将StringBuilder中的数据写入到文件中
            randomAccessFile.write(sb.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (randomAccessFile != null) {

                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //思考：将StringBuilder替换为ByteArrayOutputStream
        }
    }
}
